﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Util.Data;
using PlanningGIS.Oracle;
using ESRI.ArcGIS.Geodatabase;
using System.Runtime.InteropServices;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;

namespace PlanningGIS.Framework.Config
{
    /// <summary>
    /// 配置基础类
    /// </summary>
    public class DbConfig
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DbConfig()
            : this("")
        {

        }
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectname">连接实例名称</param>
        public DbConfig(string connectname)
        {
            ConnectName = connectname;
        }
        
        /// <summary>
        /// 连接实例名称
        /// </summary>
        private string _ConnectName = "";
        
        /// <summary>
        /// 获取或设置连接实例名称,默认为DLG
        /// </summary>
        public string ConnectName
        {
            get { return _ConnectName.Trim(); }
            set
            {
                if (!_ConnectName.Equals(value, StringComparison.CurrentCultureIgnoreCase))
                {
                    _ConnectName = value;
                    if (string.IsNullOrWhiteSpace(_ConnectName)) return;
                    Connection = DbConnectHelper.GetDbConnection(_ConnectName);
                    ReleaseSde();
                }
            }
        }

        /// <summary>
        /// 获取数据库连接实例
        /// </summary>
        public DbConnectionInfo Connection { get; protected set; }

        /// <summary>
        /// 
        /// </summary>
        private IDbContext _Context = null;

        /// <summary>
        /// 获取数据库操作对象
        /// </summary>
        public IDbContext Context
        {
            get
            {
                if (_Context == null)
                {
                    try
                    {
                        _Context = DbConnectHelper.CreateDbContext(Connection);
                    }
                    catch(Exception ex)
                    {
                        LogHelper.WriteErrorText(ex);
                    }                    
                }
                return _Context;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private IWorkspace _SdeWorkspace = null;

        /// <summary>
        /// 获取当前数据库连接Sde工作空间
        /// </summary>
        public IWorkspace SdeWorkspace
        {
            get
            {
                if (_SdeWorkspace == null)
                {
                    try
                    {
                        _SdeWorkspace = PlanningGIS.ArcGIS.WorkspaceHelper.OpenSdeWorkspaceFromString(Connection.GetSDEString());
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteErrorText(ex);
                    }
                }                
                return _SdeWorkspace;
            }
            protected set
            {
                _SdeWorkspace = value;
            }
        }

        /// <summary>
        /// 打开Sde中的数据要素类
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IFeatureClass OpenSdeFeatureClass(string name)
        {
            if (SdeWorkspace == null) return null;
            try
            {
                return GeodatabaseHelper.OpenFeatureClass(SdeWorkspace, name);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLog(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 释放Sde工作空间
        /// </summary>
        public void ReleaseSde()
        {
            try
            {
                if (_SdeWorkspace == null) return;
                Marshal.FinalReleaseComObject(_SdeWorkspace);
                _SdeWorkspace = null;
            }
            catch
            {

            }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~DbConfig()
        {
            ReleaseSde();
        }
    }
}
